package com.xysd.examproject.util;

import android.content.Context;
import android.content.pm.PackageInfo;

import com.xysd.examproject.bean.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by donghui on 2017/6/14.
 */

public class Common {
    public static ArrayList<AppInfo> getInstalledApps(Context context) {

        ArrayList<AppInfo> res = new ArrayList<AppInfo>();
        // 获取手机应用的集合
        List<PackageInfo> packs = context.getPackageManager()
                .getInstalledPackages(0);
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            // 定义应用bean对象
            AppInfo newInfo = new AppInfo();
            // 应用名
            newInfo.Appname=(p.applicationInfo.loadLabel(
                    context.getPackageManager()).toString());
            // 包名
            newInfo.packageName=(p.packageName);
            // 获取清单文件的versionName版本名
            newInfo.versionName=(p.versionName);
            // 获取清单文件的versionCode版本号
            newInfo.versionCode=(p.versionCode);
            newInfo.appIcon=p.applicationInfo.loadIcon(context.getPackageManager());
            res.add(newInfo);
        }
        return res;
    }
}
